//index.js
import{getUrl}from "../../utils/url";
//获取应用实例
const app = getApp()
let _this;
Page({
  data: {
    price: '',
    des: '',
    phone: '',
    cert:'',
    image:'',
    status:0,
    show:true,
    isUpdate:false
  },
  gotoUpdate(){
    this.setData({
      show:true,
      isUpdate:true
    })
  },

   //图片的文件上传
  choose(e){   
 
    wx.chooseImage({
      success (res) {
        const tempFilePaths = res.tempFilePaths[0]
        wx.uploadFile({
          url: getUrl()+`/approve/updoladfile`, 
          filePath: tempFilePaths,
          name:'file',
          header: {
            'Content-Type': 
             'json'
            },
          success (res){
            //this.data.msg.avatarUrl
            _this.setData({
               image:getUrl()+res.data
            })
          }
        })   }
    })
  },
  upload(name){
    if (this.data[name] != '' && this.data[name].indexOf('tmp')>0){
      wx.showLoading({
        title: '上传中',
        mask: true
      })
      wx.uploadFile({
        url: app.com.API + 'file/upload', // 仅为示例，非真实的接口地址
        filePath: this.data[name],
        name: 'file',
        formData:{
          wx_id:wx.getStorageSync("user").id,
          a_id:wx.getStorageSync("area").pk_id,
          is_temp: 0
        },
        success(res) {
          wx.hideLoading()
          let red = JSON.parse(res.data)
          if (red.code == 1) {
            
            if(name == 'cert'){
              _this.setData({
                cert: red.data.url
              })
            }else{
              _this.setData({
                stu_card: red.data.url
              })
            }
            
          }
        }
      })
    }else{
      wx.showToast({
        title: '请选择图后再上传',
        icon:'none'
      })
    }
  },
  
  formSubmit(e) {
    console.log(e);
    let formId = e.detail.formId
    if (e.detail.value.name == '') {
      wx.showToast({
        title: '请输入填姓名',
        icon: 'none'
      })
    } else if (e.detail.value.card_num == '') {
      wx.showToast({
        title: '请输入身份证号码',
        icon: 'none'
      })
    } else if (!this.data.image) {
      wx.showToast({
        title: '请上传学生证',
        icon: 'none'
      })
    }else {
    let user=  wx.getStorageSync('user');
    let UrL="";
      wx.request({
        url: getUrl()+`/approve/info/${user.id}`,  //路径
        success: function(res) {
          console.log(res.data);
          let approve={};
         if(res.data!=''){
          UrL="/approve/update"
          approve.id=res.data.id;
        }else{
          UrL="/approve/save"
        }
     let user=wx.getStorageSync('user');
      approve.name=e.detail.value.name;
      approve.cardNum=e.detail.value.cardNum;
      approve.image=_this.data.image;       
      approve.userId=user.id;
     
        wx.request({
          url: getUrl()+UrL,  //路径
         data:approve,//参数 
          success: function(res) {
       wx.showToast({
         title: '申请上传成功',
       })
          }
        })
     
        



        }
      })
   
      
    }
  },

  makePhone(){
    wx.makePhoneCall({
      phoneNumber: wx.getStorageSync("dl").phone,
    })
  },
  onLoad: function (options) {
    _this = this
    let server = wx.getStorageSync("server")
    for(let i in server){
      server[i].pk_server = ((server[i].dl_sy + server[i].p_sy)*100).toFixed(0)
    }
    this.setData({
      server: server
    })
  },
  onShow(){
      this.getRes()
      let user=  wx.getStorageSync('user');
      wx.request({
        url: getUrl()+`/approve/info/${user.id}`,  //路径
        success: function(res) {
          if(res.data!=''){
         _this.setData({
           show:false,
         isUpdate:false,
        status:res.data.status
    })
          }
  
   
  }
      })
  
    
  },
  getRes() {
    wx.showLoading({
      title: '加载中',
      task:true
    })
   let user= wx.getStorageSync('user');
      wx.hideLoading()
        //判断是否同意协议
      if (!wx.getStorageSync("xy")) {
        wx.navigateTo({
              url: '/pages/mine/jdsm/jdsm',
            })
          } 
     

/*         wx.showToast({
          title: '失败',
          icon: 'none'
        }) */
  },


})
